package com.handtransaction.controller;


import com.handtransaction.entity.Administrator;
import com.handtransaction.entity.Goods;
import com.handtransaction.entity.OSGoods;
import com.handtransaction.service.GoodsService;
import com.handtransaction.service.OffShelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OffShelfController {
    @Qualifier("offShelfImpl")
    @Autowired
    private OffShelfService oSService;
    @Autowired
    private GoodsService goodsService;

    //进入下架商品原因编写页面
    @RequestMapping("/showOSGoodsDetail")
    public String showOSGoodsDetail(Integer gid, Model model){
        Goods good = goodsService.QueryGoodBygid(gid);
        model.addAttribute("OSgood",good);
        return"admin/offShelfGoods";
    }

    //确认下架商品
    @RequestMapping("/offShelfGoods")
    public String offShelfGoods(HttpSession session , Integer gid , String reason){
        Administrator admin = (Administrator) session.getAttribute("admin");
        oSService.createOffShelfGoods(admin,gid,reason);
        goodsService.DeleteGoodsByGid(gid);
        return "redirect:ShowALLGoods";
    }

    //管理员通过举报信息来下架商品
    @RequestMapping("/offShelfTipOffGoods")
    public String offShelfTipOffGoods(HttpSession session , Integer gid , Model model){
        Administrator admin = (Administrator) session.getAttribute("admin");
        oSService.createOffShelfGoods(admin,gid,"管理员下架举报商品");
        goodsService.DeleteGoodsByGid(gid);
        return "redirect:showAllTOGoods";
    }

    //查看所有已下架的商品
    @RequestMapping("/showAllOffShelfGoods")
    public String showAll(Model model){
        List<OSGoods> osGoodsList = oSService.queryAllOSGoods();
        model.addAttribute("osGoodsList" , osGoodsList);
        return "admin/allOSGoods";
    }


}
